﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SingletonPattern.utils
{
    class Number
    {
        public static int MAX_SCORE = 100;
        public static double MAX_ANGLE = 180.0;
        public static int MAX_ANGLE_INT = 180;
        public static int MAX_GRAY_SCALE = 255;
        public static int LEAD_MIN_NUMBER = 2;
        public static int CAMERA_INTERVAL_MIN = 50;
        public static int ERROR_MIN_NUMBER = 1;
        /// <summary>
        /// generate random number from min to max
        /// </summary>
        /// <remarks>trunghpb@vnext.vn</remarks>
        /// <param name="min"></param>
        /// <param name="max"></param>
        /// <returns>int</returns>
        public static int genRandom(int min, int max)
        {
            Random random = new Random();
            return random.Next(min, max);
        }

        /// <summary>
        /// generate random number from 0 to max
        /// </summary>
        /// <remarks>trunghpb@vnext.vn</remarks>
        /// <param name="min"></param>
        /// <param name="max"></param>
        /// <returns>int</returns>
        public static int genRandom(int max)
        {
            return Number.genRandom(0, max);
        }

        /// <summary>
        /// Convert string (containing some numbers separated by space) to a number list
        /// </summary>
        /// <param name="str">string</param>
        /// <returns>number list</returns>
        /// <remarks>trungpv@vnext.vn</remarks>
        public static List<int> strToList(string str)
        {
            var array = str.Split((string[])null, StringSplitOptions.RemoveEmptyEntries);
            List<int> lsNumbers = array.Select(arg => int.Parse(arg)).ToList();
            return lsNumbers;
        }

        public static bool isDoubleBounded(string s, double min, double max)
        {
            double value;
            if (!double.TryParse(s, out value) || value < min || value > max)
                return false;
            return true;
        }

        /// <summary>
        /// Check if a string is a number bounded by upper and lower bounds
        /// </summary>
        /// <param name="s">string</param>
        /// <param name="min">lower bound</param>
        /// <param name="max">upper bound</param>
        /// <returns>true or false</returns>
        /// <remarks>trungpv@vnext.vn</remarks>
        public static bool isNumberBounded(string s, int min, int max)
        {
            int value;
            if (!int.TryParse(s, out value) || value < min || value > max)
                return false;
            return true;
        }

        /// <summary>
        /// Check a number with lower bound
        /// </summary>
        /// <remarks>trungpv@vnext.vn</remarks>
        public static bool isNumberBoundedBelow(string s, int min)
        {
            return isNumberBounded(s, min, int.MaxValue);
        }

        /// <summary>
        /// Check a natural number with upper bound
        /// </summary>
        /// <remarks>trungpv@vnext.vn</remarks>
        public static bool isNaturalNumberBoundedAbove(string s, int max)
        {
            return isNumberBounded(s, 0, max);
        }

        /// <summary>
        /// Check a natural number
        /// </summary>
        /// <remarks>trungpv@vnext.vn</remarks>
        public static bool isNaturalNumber(string s)
        {
            return isNumberBounded(s, 0, int.MaxValue);
        }
    }
}
